package com.example.my_bookkeeping.data.manager;

import java.util.ArrayList;

public class CategoryManager extends Manager {
    private static final CategoryManager instance = new CategoryManager();
    private final ArrayList<String> incomeCategoryList = new ArrayList<>();
    private final ArrayList<String> expenseCategoryList = new ArrayList<>();

    private CategoryManager() {
        // 初始化默认分类（保持原有逻辑）
        incomeCategoryList.add("工资");
        incomeCategoryList.add("奖金");
        expenseCategoryList.add("出行");
        expenseCategoryList.add("餐饮");
    }

    public static CategoryManager getInstance() {
        return instance;
    }

    public boolean addIncomeCategory(String category) {
        if (!incomeCategoryList.contains(category)) {
            boolean success =  incomeCategoryList.add(category);
            notifyObservers();
            return success;
        }
        return false;
    }

    public boolean addExpenseCategory(String category) {
        if (!expenseCategoryList.contains(category)) {
            boolean success =   expenseCategoryList.add(category);
            notifyObservers();
            return success;
        }
        return false;
    }

    public ArrayList<String> getIncomeCategoryList() {
        return new ArrayList<>(incomeCategoryList);
    }

    public ArrayList<String> getExpenseCategoryList() {
        return new ArrayList<>(expenseCategoryList);
    }

    public ArrayList<String> getAllCategoryList() {
        ArrayList<String> allCategoryList = new ArrayList<>();
        allCategoryList.addAll(incomeCategoryList);
        allCategoryList.addAll(expenseCategoryList);
        return allCategoryList;
    }

    public void updateCategory(String oldName, String newName) {
        // 在收入分类中查找并更新
        int incomeIndex = incomeCategoryList.indexOf(oldName);
        if (incomeIndex != -1) {
            incomeCategoryList.set(incomeIndex, newName);
            notifyObservers();
            return;
        }
        
        // 在支出分类中查找并更新
        int expenseIndex = expenseCategoryList.indexOf(oldName);
        if (expenseIndex != -1) {
            expenseCategoryList.set(expenseIndex, newName);
            notifyObservers();
        }
    }

    public void moveCategory(String categoryName, boolean isIncome) {
        // 从原列表中移除
        incomeCategoryList.remove(categoryName);
        expenseCategoryList.remove(categoryName);
        
        // 添加到目标列表
        if (isIncome) {
            if (!incomeCategoryList.contains(categoryName)) {
                incomeCategoryList.add(categoryName);
                notifyObservers();
            }
        } else {
            if (!expenseCategoryList.contains(categoryName)) {
                expenseCategoryList.add(categoryName);
                notifyObservers();
            }
        }
    }

    public boolean isIncomeCategory(String categoryName) {
        return incomeCategoryList.contains(categoryName);
    }

    public void deleteCategory(String category, boolean isIncome) {
        if (isIncome) {
            incomeCategoryList.remove(category);
        } else {
            expenseCategoryList.remove(category);
        }
        notifyObservers();
    }

    public void clear() {
        incomeCategoryList.clear();
        expenseCategoryList.clear();
    }

    public void reset() {
        clear();
        incomeCategoryList.add("工资");
        incomeCategoryList.add("奖金");
        expenseCategoryList.add("出行");
        expenseCategoryList.add("餐饮");
    }
}
